package com.jiazhong.example.project.bookstore.backend.controller;

import com.jiazhong.example.project.bookstore.backend.service.UserService;
import com.jiazhong.example.project.bookstore.commons.bean.User;
import com.jiazhong.example.project.bookstore.commons.util.JsonResult;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/{page}")
    public JsonResult find(@PathVariable("page") int page) {
        return userService.find(page);
    }

    @GetMapping("/find_id/{id}")
    public JsonResult findById(@PathVariable("id") String id) {
        return userService.findUserById(id);
    }

    @PostMapping
    public JsonResult save(User user) {
        return userService.saveUser(user);
    }

    @PutMapping("/status/{id}/{status}")
    public JsonResult updateStatus(@PathVariable("id") String id, @PathVariable("status") int status) {
        return userService.changeStatus(id, status);
    }

    @PutMapping
    public JsonResult update(User user) {
        return userService.updateUser(user);
    }

    @PostMapping("/login")
    public JsonResult login(String username, String password) {
        return userService.login(username, password);
    }

    /*@GetMapping("/is_login/{id}")
    public JsonResult isLogin(@PathVariable("id") String id) {
        return userService.isLogin(id);
    }*/

    @GetMapping("/is_login")
    public JsonResult isLogin(HttpServletRequest request) {
        return userService.isLogin(request.getHeader("token"));
    }

}
